//
//  FMListDatailViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FMListDatailViewController.h"
#import "FMListDateilTableViewCell.h"
#import "FMListDateilModel.h"
#import "FMListRadioDateilModel.h"
#import "FMPlayViewController.h"

@interface FMListDatailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *FMArr;


@end

@implementation FMListDatailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    [self createView];
    
}

- (void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/topicset/android/radio/%@/0-20.html", self.cid];
    NSLog(@"%@", urlStr);
    [xwzAFNetTool getNetWithURL:urlStr body:nil headFile:nil responStyle:XWZ_JSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *arr = [responseObject objectForKey:@"tList"];
        self.FMArr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in arr) {
            FMListDateilModel *model = [[FMListDateilModel alloc] initWithDic:tempDic];
            NSMutableDictionary *tempDicRo = [tempDic objectForKey:@"radio"];
            FMListRadioDateilModel *radioModel = [[FMListRadioDateilModel alloc] initWithDic:tempDicRo];
            model.radio = radioModel;
            [self.FMArr addObject:model];
        }
        
       
        [self.tableV reloadData];
        [self.tableV.mj_header endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"FM数据解析失败");
    }];
    
    
}


- (void)createView
{
    [self createTableView];

}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@0);
        make.right.equalTo(self.view).with.offset(0);
        make.width.equalTo(self.view);
        make.height.equalTo(self.view);
        
    }];
    
    
    [self createPullToFresh];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.FMArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cellS";
    FMListDateilTableViewCell *cellS = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cellS) {
        cellS = [[FMListDateilTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
   
    if (0 != self.FMArr.count) {
        FMListRadioDateilModel *radioModel = [[self.FMArr objectAtIndex:indexPath.row] radio];
        cellS.labelTitle.text = radioModel.title;
        cellS.labelName.text = radioModel.tname;
        cellS.imageL.image = [UIImage imageNamed:@"tingyinle"];
        [cellS.imageV sd_setImageWithURLStr:radioModel.imgsrc placeholderImage:[UIImage imageNamed:@"lol-tupian"]];
        FMListDateilModel *detailModel = [self.FMArr objectAtIndex:indexPath.row];
        NSString *num = [NSString stringWithFormat:@"%@", detailModel.playCount];
        cellS.labelNum.text = num;
   
    }
    
    return cellS;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150 * HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   FMPlayViewController *playVC = [FMPlayViewController sharePlayerViewController];
    [playVC.player stop];
    FMListRadioDateilModel *radioModel = [[self.FMArr objectAtIndex:indexPath.row] radio];
    playVC.docid = radioModel.docid;
    playVC.tid = [[self.FMArr objectAtIndex:indexPath.row] tid];
    playVC.indexNext = 0;
    playVC.indexLast = 0;
  
    [self.navigationController pushViewController:playVC animated:YES];
}

- (void)createPullToFresh
{
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.tableV.mj_header beginRefreshing];
}

- (void)loadNewData
{
    [self getData];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
